﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.ArticleCategories
{
    public partial class Edit : ManageBasePage
    {
        private IArticleCategoryTask _articleCatTask;
        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("ArticleCategoryIndex");
            _articleCatTask = GetInstance<IArticleCategoryTask>();

            if (!IsPostBack)
            {
                var id = 0;
                if (Request.QueryString["Id"] != null && Int32.TryParse(Request.QueryString["Id"], out id))
                {
                    var info = _articleCatTask.Get(id);
                    if (info == null)
                    {
                        ShowMsgAndReturn("参数错误");
                        return;
                    }

                    txtId.Text = info.Id.ToString();
                    txtCategoryName.Text = info.CategoryName;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var catName = txtCategoryName.Text.Trim();
            if (string.IsNullOrEmpty(catName))
            {
                ShowMsg("输入不完整");
                return;
            }

            var cmd = new ArticleCategoryCommand
                          {
                              ArticleCategoryId = Convert.ToInt32(txtId.Text),
                              CategoryName = catName
                          };
            using (var work = UnitOfWork.Begin())
            {
                if (_articleCatTask.Save(cmd) != null)
                {
                    work.Commit();
                    ShowMsg("保存成功", "Index.aspx");
                }
                else
                {
                    ShowMsg("保存失败");
                }
            }
        }
    }
}